using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Service.WeiXinPay.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;

namespace Admin.NET.Application.Service.WeiXinPay;

/// <summary>
/// 微信服务商配置
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class WeiXinPayService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<WxPayProvider> _rep;

	public WeiXinPayService(SqlSugarRepository<WxPayProvider> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 保存微信配置
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/wxpay/editWxPay")]
	public async Task EditWxPay(WxPayInput input)
	{
		WxPayProvider entity = input.Adapt<WxPayProvider>();
		if (input.Id.HasValue)
		{
			WxPayProvider model = await _rep.GetFirstAsync((WxPayProvider u) => (long?)u.Id == input.Id);
			if (model == null)
			{
				throw Oops.Bah("当前数据不存在");
			}
			if (entity.AppId != null && !entity.AppId.Contains("*"))
			{
				model.AppId = entity.AppId;
			}
			if (entity.MerchantId != null && !entity.MerchantId.Contains("*"))
			{
				model.MerchantId = entity.MerchantId;
			}
			if (entity.MerchantV3Secret != null && !entity.MerchantV3Secret.Contains("*"))
			{
				model.MerchantV3Secret = entity.MerchantV3Secret;
			}
			if (entity.MerchantCertificateSerialNumber != null && !entity.MerchantCertificateSerialNumber.Contains("*"))
			{
				model.MerchantCertificateSerialNumber = entity.MerchantCertificateSerialNumber;
			}
			if (entity.MerchantCertificatePrivateKey != null && !entity.MerchantCertificatePrivateKey.Contains("*"))
			{
				model.MerchantCertificatePrivateKey = entity.MerchantCertificatePrivateKey;
			}
			if (entity.RsaPublicKey != null && !entity.RsaPublicKey.Contains("*"))
			{
				model.RsaPublicKey = entity.RsaPublicKey;
			}
			model.Remark = entity.Remark;
			model.WechatPayUrl = entity.WechatPayUrl;
			await _rep.UpdateAsync(model);
		}
		else
		{
			await _rep.InsertOrUpdateAsync(entity);
		}
	}

	/// <summary>
	/// 查询微信配置
	/// </summary>
	/// <returns></returns>
	[HttpGet("/wxpay/list/")]
	public async Task<WxPayProvider> GetWxPay()
	{
		WxPayProvider model = await _rep.GetFirstAsync((WxPayProvider u) => true);
		string pattern = "(?<=[\\S]{4})\\S(?=[\\S]{4})";
		Regex rgx = new Regex(pattern);
		if (model != null)
		{
			if (model.AppId != null && model.AppId.Length > 8)
			{
				model.AppId = rgx.Replace(model.AppId, "*");
			}
			if (model.MerchantId != null && model.MerchantId.Length > 8)
			{
				model.MerchantId = rgx.Replace(model.MerchantId, "*");
			}
			if (model.MerchantV3Secret != null && model.MerchantV3Secret.Length > 8)
			{
				model.MerchantV3Secret = rgx.Replace(model.MerchantV3Secret, "*");
			}
			if (model.MerchantCertificateSerialNumber != null && model.MerchantCertificateSerialNumber.Length > 8)
			{
				model.MerchantCertificateSerialNumber = rgx.Replace(model.MerchantCertificateSerialNumber, "*");
			}
			if (model.MerchantCertificatePrivateKey != null && model.MerchantCertificatePrivateKey.Length > 8)
			{
				model.MerchantCertificatePrivateKey = rgx.Replace(model.MerchantCertificatePrivateKey, "*");
			}
			if (model.RsaPublicKey != null && model.RsaPublicKey.Length > 8)
			{
				model.RsaPublicKey = rgx.Replace(model.RsaPublicKey, "*");
			}
		}
		return model;
	}

	/// <summary>
	/// 删除配置
	/// </summary>
	/// <returns></returns>
	[HttpPost("/wxpay/delete")]
	public async Task Deletewx()
	{
		await _rep.DeleteAsync((WxPayProvider d) => true);
	}
}
